﻿using Ewelve_Four_HD.Model;
using MD5Hash;

namespace Ewelve_Four_HD.DAL
{
    public class UserDal
    {
        private readonly Mycontext context;

        public UserDal(Mycontext _context)
        {
            context = _context;
        }
        /// <summary>
        /// 用户添加
        /// </summary>
        /// <param name="X"></param>
        /// <returns></returns>
        public int UserAdd(UserInfo X)
        {
            try
            {
                var F = context.Users.Count(O => O.UserName == X.UserName);
                if (F > 0)
                {
                    return -1;
                }
                X.Password = X.Password.GetMD5();
                context.Users.Add(X);
                return context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public dynamic UserLog(string UserName, string Password)
        {
            try
            {
                var F = context.Users.Count(O => O.UserName == UserName);
                if (F == 0)
                {
                    return -2;
                }
                Password = Password.GetMD5();
                var D = context.Users.Count(O => O.Password == Password);
                if (D == 0)
                {
                    return -1;
                }
                return context.Users.Count(O => O.UserName == UserName&& O.Password == Password);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
